<template>
  <div class="app-wrapper">
    <headerTitle
      :titleList="titleList"
      :hideButton="false"
      :btnTitle="'新建'"
      icon="iconfont icon-huiyuanzhucedengluguanli"
      @handelClick="() => add()"
    />
    <div class="app-container"  ref="block">
      <div class="containerMain">
        <div class="search-item">
          <div class="item-list">
            <div class="item-con">
              <span>名称</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.sessionId"
                placeholder="全部"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="视频面签" value="1"></el-option>
                <el-option label="法人开户" value="2"></el-option>
              </el-select>
            </div>
            <div class="item-con">
              <span>关联业务</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.sessionId"
                placeholder="全部"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="人脸识别" value="1"></el-option>
                <el-option label="风险提示" value="2"></el-option>
                <el-option label="声明朗读" value="3"></el-option>
                <el-option label="客户签字" value="4"></el-option>
              </el-select>
            </div>
            <div class="item-con">
              <span>技能组</span>
              <el-select
                size="mini"
                clearable
                v-model="searchform.sessionId"
                placeholder="全部"
              >
                <el-option label="全部" value=""></el-option>
                <el-option label="技能组1" value="1"></el-option>
                <el-option label="技能组2" value="2"></el-option>
              </el-select>
            </div>
          </div>
          <div class="item-btn">
            <el-button
              class="kyeup_search_btn"
              type="primary"
              size="mini"
              @click="() => fetchData()"
              >搜索</el-button
            >
            <el-button type="default" size="mini" @click="() => reset()"
              >重置</el-button
            >
          </div>
        </div>
        <div class="container">
          <div class="div-table">
            <el-table
              :header-cell-style="headerStyle"
              :data="list"
              element-loading-text="Loading"
              fit
              size="mini"
              highlight-current-row
              :height="tableHeight"
            >
              <el-table-column label="名称" width="150">
                <template slot-scope="scope">{{ scope.row.sessionId }}</template>
              </el-table-column>
              <el-table-column label="关联业务">
                <template slot-scope="scope">{{
                  scope.row.sessionId
                }}</template>
              </el-table-column>
              <el-table-column label="技能组">
                <template slot-scope="scope">{{ scope.row.sessionId }}</template>
              </el-table-column>
              <el-table-column label="操作" width="200px">
                <template slot-scope="scope">
                  <el-button type="text" @click="() => editBtn(scope.row)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="text"  @click="() => delFn(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <pagination
      :exportShow="true"
      :type="'primary'"
      @handelExport="exportDetail"
      btnExport="导出"
      :pagination="pagination"
    />


  </div>
</template>

<script>
import {  fetchAllSkill} from "@/api/systemapi/skill";

import moment from "moment";
import Pagination from "@/components/Pagination/index";
import HeaderTitle from "@/components/Headertitle/index";
import {
  fetchAgentList
} from "@/api/systemapi/agent";
import {
  fetchList,fetchDetailExport, fetchAttendeeDetail,fetchDetailExportCheck
} from "@/api/systemapi/video";
import {
  fetchAll
} from "@/api/systemapi/skill";
import { pageParams } from "@/settings";

export default {
  components: {
    Pagination,
    HeaderTitle
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: "启用",
        2: "禁用",
      };
      return statusMap[status];
    },
  },
  data() {
    const TODAYSTART = moment().startOf('day').format('YYYY-MM-DD 00:00:00')
    const TODAYEND = moment().endOf('day').format('YYYY-MM-DD 23:59:59')
    return {
      titleList: [{ title: "系统设置" } ,{ title: "合同管理" }],
      list:[],
      skillList:[],
      tableHeight: "300", //表格默认高度
      timeValue: [TODAYSTART, TODAYEND],
      listLoading: true,
      skillGroupList: [],
      searchform: {
        rows: "10",
        page: "1",
        sTime:"",
        eTime:"",
        sessionId:"",
      },
      // 分页
      pagination: pageParams,
    };
  },
  watch: {
    // 分页
    "pagination.currentPage": function () {
      this.searchform.page = this.pagination.currentPage;
      this.fetchData();
    },
    "pagination.pageSize": function () {
      this.searchform.rows = this.pagination.pageSize;
      this.searchform.page = 1;
      this.fetchData();
    },
  },
  mounted() {
    this.$nextTick(() => {
      let self = this;
      this.tableHeight = this.$refs.block.offsetHeight - 220;
      window.onresize = () => {
        return (() => {
          this.tableHeight = this.$refs.block.offsetHeight - 220;
        })();
      };
    });
    this.fetchData()
  },
  methods: {
    //表头背景颜色修改
    headerStyle({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:#F5F6FB;color:#666";
      }
    },
    // 重置搜索，刷新列表
    reset() {
      const TODAYSTART = moment().startOf('day').format('YYYY-MM-DD 00:00:00')
      const TODAYEND = moment().endOf('day').format('YYYY-MM-DD 23:59:59')
      this.timeValue = [TODAYSTART, TODAYEND],
      this.searchform = {
        sTime:this.timeValue[0],
        eTime:this.timeValue[1],
        sessionId:"",
        rows: "10",
        page: "1",
      }
    },
    // 删除
    delFn(data) {
      this.$confirm(`${data.level==2?`是否确定删除二级机构${data.name}及其下属机构`:`是否确定删除三级机构${data.name}`}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          fetchDel({id: data.id}).then(response => {
            if (response.code === 0) {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.fetchData();
            } else {
              this.$message({
                message: response.msg,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //导出
    exportDetail() {
        if (this.timeValue != null && this.timeValue.length > 0) {
          this.searchform.sTime = moment(this.timeValue[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.searchform.eTime = moment(this.timeValue[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
      fetchDetailExportCheck({ ...this.searchform }).then(response => {
        if(response.code == 0){
          fetchDetailExport({ ...this.searchform }).then((res) => {
            console.log(res);
            let blob = new Blob([res.data], {
              type: "application/vnd.ms-excel",
            });
            let str = res.headers["content-disposition"].split(";")[1];
            // console.log(str,"str1111")
            if (str.indexOf("=") != -1) {
              var filename = decodeURI(str.slice(9));
            } else {
              var filename = decodeURI(str);
            }
            var downloadElement = document.createElement("a");
            var href = window.URL.createObjectURL(blob); //创建下载的链接
            downloadElement.href = href;
            downloadElement.download = filename; //下载后文件名
            document.body.appendChild(downloadElement);
            downloadElement.click(); //点击下载
            document.body.removeChild(downloadElement); //下载完成移除元素
            window.URL.revokeObjectURL(href); //释放掉blob对象
          });
        }else{
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });

    },
    //搜索
    fetchData() {
      console.log(this.timeValue,"this.timeValue");
      if (this.timeValue != null && this.timeValue.length > 0) {
          this.searchform.sTime = moment(this.timeValue[0]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
          this.searchform.eTime = moment(this.timeValue[1]).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        }
        console.log(this.searchform.sTime,"231232312");

      fetchList({...this.searchform}).then((response) => {
        if (response.code == 0) {
          this.list = response.data.rows;
          // this.pagination.total = response.data.records;
          console.log(response.data.rows)
          console.log(this.list)
        } else {
          this.$message({
            type: "error",
            message: response.msg,
          });
        }
      });
    },
    // 删除
    del(data) {
      this.$confirm("是否确定注销?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        fetchDel(data.id).then((response) => {
          if (response.code === 0) {
            this.fetchData();
            this.$message({
              message: "注销成功",
              type: "success",
            });
          } else {
            this.$message({
              message: response.msg,
              type: "error",
            });
          }
        });
      });
    },
    // 新增
    add(data) {
      this.$router.push({
        path: "/vpm/readstatemanage/detail",
        // query: { type: 3},
      });
    },
    //编辑
    editBtn(data) {
      this.$router.push({
        path: "/system/agent/detail",
        // query: { type: "2", id: data.id},
      });
    },
  },
};
</script>
<style lang="scss">
.tree-con {
  .el-tree-node__content {
    margin-bottom: 20px;
  }
}
</style>
<style lang="scss" scoped>
.containerMain {
  .search-item {
    padding: 20px 10px;
    font-size: 14px;
    color: #666;
    display: flex;
    justify-content: space-between;
    .item-list {
      display: flex;
      flex-wrap: wrap;
    }
    .item-con {
      display: flex;
      margin-right: 30px;
      margin-top: 20px;

      span {
        display: inline-block;
        line-height: 28px;
        margin-right: 10px;
      }
    }
    .item {
      width: 180px;
    }
  }
  .container {
    display: flex;
    flex-direction: row;
    .div-tree {
      width: 250px;
    }
    .div-table {
      flex: 1;
      width: 100%;
      overflow: hidden;
      .el-table {
        height: 100%;
        width: 100%;
        overflow: auto;
      }
    }
    .el-row {
      height: 300px;
      .el-col {
        height: 100%;
        .div-tree {
          height: 90%;
          .el-tree {
            height: 100%;
            overflow-y: auto;
          }
        }
      }
    }
  }
  .item-btn {
    margin-top: 20px;
    flex: 0 0 140px;
  }
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
